#include "OccShapeSelector.h"
#include <qDebug.h>
#include <QMessageBox>

OccShapeSelector::OccShapeSelector()
{
}

OccShapeSelector::~OccShapeSelector() {

}

bool OccShapeSelector::addSelectedShape(const TopoDS_Shape& shape) {
    auto copyShape = shape;

    /*if (shape.ShapeType() != TopAbs_SHELL) {
        qDebug() << "WARNING: trying to delete a non-Shape shape via ShapeSelector.";
        return false;
    }*/
    auto it = std::find_if(m_selectedShapes.begin(), m_selectedShapes.end(),
        [&shape](const TopoDS_Shape& ref) {
            return ref == shape;
        });
    if (it != m_selectedShapes.end()) {
        return false;
    }
    m_selectedShapes.push_back(copyShape);
    return true;
}

bool OccShapeSelector::deleteSelectedShape(const TopoDS_Shape& shape) {
    // 查找 shape 是否存在于 m_selectedShapes 中
    auto it = std::find_if(m_selectedShapes.begin(), m_selectedShapes.end(),
        [&shape](const TopoDS_Shape& ref) {
            return ref == shape;
        });
    // 如果找到了 shape
    if (it != m_selectedShapes.end()) {
        // 从向量中删除 shape
        m_selectedShapes.erase(it);
        return true; // 表示成功删除
    }

    return true;
}


std::vector<TopoDS_Shape> OccShapeSelector::getSelectedShapes() {
    return m_selectedShapes;
}

const TopoDS_Solid& OccShapeSelector::getFirstSelected() {
    if (m_selectedShapes.empty()) {
        qDebug() << "[ERROR]: Getting shape from empty list.";
    }
    return TopoDS::Solid(m_selectedShapes[0]);
}


bool OccShapeSelector::isDataValid() {
    if (m_selectedShapes.empty()) return false;
    for (auto item : m_selectedShapes) {
        if (item.ShapeType() != TopAbs_SOLID) return false;
    }
    return true;
}

void OccShapeSelector::clearSelectedShape() {
    m_selectedShapes.clear();
    //TODO: 发送信号？
}

const TopoDS_Shape& OccShapeSelector::getSelectedShape() {
    if (isDataValid()) {
        return m_selectedShapes[0];
    }
}

